﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Xinyi.Common;
using Xinyi.Data;
using System.Collections;

namespace che.admin
{
    public partial class user_mod : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FunctionClass.CheckAdminLogin(0);
            if (!IsPostBack)
            {
                BindData();
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            string strID, strSql, strAreaID1;

            strAreaID1 = "0";
            strID = Request.QueryString["ID"];
            if (!FunctionClass.CheckStr(strID, 1))
            {
                FunctionClass.ShowMsgBox("URL错误！");
                Response.End();
            }

            DataClass myData = new DataClass();
            SqlConnection myConn = myData.ConnOpen();

            strSql = "select * from T_User where ID=" + strID;
            SqlDataReader myDr = myData.GetSqlDataReader(strSql, myConn);
            if (myDr.Read())
            {
                tbUserName.Text = myDr["UserName"].ToString();
                tbPassWord.Text = myDr["PassWord"].ToString();
                tbClientName.Text = myDr["ClientName"].ToString();
                rblSex.SelectedValue = myDr["Sex"].ToString();
                tbLinkman.Text = myDr["Linkman"].ToString();
                tbTel.Text = myDr["Tel"].ToString();
                tbMobile.Text = myDr["Mobile"].ToString();
                tbMail.Text = myDr["Mail"].ToString();
                tbAddress.Text = myDr["Address"].ToString();
                
                cblIsLock.SelectedValue = myDr["IsLock"].ToString();
                tbIntro.Text = myDr["Intro"].ToString();
            }
            myDr.Close();
            myDr.Dispose();

            myData.ConnClose(myConn);
        }

        protected void btSubmit_Click(object sender, EventArgs e)
        {
            string strID, strUserName, strPassWord, strClientName, strSex, strLinkman, strTel, strMobile, strAddress, strMail;
            string strIsLock, strIntro, strSql;

            strID = Request.QueryString["ID"];
            if (!FunctionClass.CheckStr(strID, 1))
            {
                FunctionClass.ShowMsgBox("URL错误！");
                Response.End();
            }

            strUserName = tbUserName.Text;
            strPassWord = tbPassWord.Text;
            strClientName = tbClientName.Text;
            strSex = rblSex.SelectedValue;
            strLinkman = tbLinkman.Text;
            strTel = tbTel.Text;
            strMobile = tbMobile.Text;
            strAddress = tbAddress.Text;
            strMail = tbMail.Text;

            strIsLock = cblIsLock.SelectedValue;
            strIntro = tbIntro.Text;

            //判断手机号码是否11位
            if (strMobile.Length != 11)
            {
                FunctionClass.ShowMsgBox("手机号码必须是11位数字！前面请不要加0或其他字符");
                Response.End();
            }

            DataClass myData = new DataClass();
            SqlConnection myConn = myData.ConnOpen();

            strSql = "update T_User set UserName=@UserName,PassWord=@PassWord,ClientName=@ClientName,Sex=@Sex,Linkman=@Linkman"
                + ",Tel=@Tel,Mobile=@Mobile,Address=@Address,Mail=@Mail"
                + ",IsLock=@IsLock,Intro=@Intro where ID=@ID";
            string[] ParamsName = new string[] { "@UserName", "@PassWord", "@ClientName", "@Sex", "@Linkman", "@Tel", 
                    "@Mobile","@Address","@Mail","@IsLock","@Intro","@ID" };
            string[] ParamsValue = new string[] { strUserName, strPassWord, strClientName, strSex, strLinkman, strTel, 
                    strMobile,strAddress,strMail, strIsLock, strIntro, strID };

            //插入数据库
            myData.InsertData(strSql, ParamsName, ParamsValue, myConn);

            myData.ConnClose(myConn);

            FunctionClass.ShowMsgBox("修改成功！", "");
            Response.End();
        }
    }
}
